/******************************************************************************
ISYS 1078 / 1079 — Information Retrieval
Assignment #1

Student Name: [ Rui, Wang ]
Student ID: [ 3171459 ]
CSIT username: [ rwang ]
*******************************************************************************/

#define W_MAX_LEN 100
#define INIT_LEN 50
#define SUCCESS 1
#define FAILURE 0
#include <stdio.h>

typedef enum ColorType{ Red, Black } ColorType;

typedef struct docNode *DocTree;
typedef struct docNode{
	int docId;
	char *docNum;
	DocTree Right;
	DocTree Left;
	
}DocNode;

typedef struct docHead{
	DocTree root;
}DocIndex;


typedef struct posNode PosElement;
typedef struct posNode *PosList;
typedef struct posNode{
	int pos;
	PosList next;
}PosNode;

typedef struct ivtNode IvtElement;
typedef struct ivtNode *IvtList;
typedef struct ivtNode{
	int docId;
	int freq_dt;
	PosList head;
	IvtList next;
}IvtNode;

typedef struct ivtHead{
	IvtList head;
}IvtIndex;

typedef struct termNode LexElement;
typedef struct termNode *LexTree;
typedef struct termNode{
	char *term;
	int freq_t;
	IvtList head;
	LexTree Right;
	LexTree Left;
	LexTree parent;
	ColorType Color;
	
	int currDocId;
	IvtList currIvtPtr;
}TermNode;

typedef struct lexHead{
	LexTree head;
}LexIndex;

typedef struct term{
/*	char *term;*/
	char term[INIT_LEN];
	int ppos;
}Term;

typedef struct doc{
	int docId;
	char docNum[INIT_LEN];
}Doc;


void DocIndexInit(DocIndex *docIndex);
int DataParse(int mode, char *filename, DocIndex *docIndex, LexIndex *lexIndex);
void DocInsert(DocIndex *docIndex, int id, char *num);
void GenerateDocMap(DocTree DT, FILE *fp, Doc *doc);

void LexIndexInit(LexIndex *lexIndex);
void TermInsert(LexIndex *lexIndex, char *word, LexElement** newTerm, int docId);
int IvtListInsert(LexElement* term, int docId, int pos);
void GenerateLexAndIvtFile(LexTree LT, FILE *fp_lex, FILE *fp_ivt, Term *term);
void GenerateIvtLists(IvtList IL, FILE *fp, int freq_t);
void GenerateFiles(DocIndex *docIndex, LexIndex *lexIndex);

void DestroyTask(DocIndex *docIndex, LexIndex *lexIndex);
void DestroyDocTree(DocTree DT);
void DestroyLexTree(LexTree LT);
void DestroyIvtList(IvtList IL);
void DestroyPosList(PosList PL);

void ToLowerCase(char *str);
int IsAlpha(char c);
int IsDigit(char c);
int IsDelimiter(char c);
